package com.liunian.config;

import com.liunian.IdGenerator;
import com.liunian.discovery.RegistryConfig;
import com.liunian.loadbalancer.LoadBalancer;
import com.liunian.loadbalancer.impl.RoundRobinLoadBalancer;
import com.liunian.protection.CircuitBreaker;
import com.liunian.protection.RateLimiter;
import com.liunian.protection.TokenBuketRateLimiter;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 全局的配置类，代码配置 -> xml配置 -> 默认项
 */
@Data
@Slf4j
public class Configuration {

    // 配置信息 --> 端口号
    private int port = 8091;
    // 配置信息 --> 应用程序的名字
    private String appName = "default";
    // 配置信息 --> 注册中心
    private RegistryConfig registryConfig = new RegistryConfig("zookeeper://127.0.0.1:2181");
    // 配置信息 --> 序列化方式
    private String serializeType = "jdk";
    // 配置信息 --> 压缩方式
    private String compressType = "gzip";
    // 配置信息 --> id发号器
    private IdGenerator idGenerator = new IdGenerator(1, 2);
    // 配置信息 --> 负载均衡策略
    private LoadBalancer loadBalancer = new RoundRobinLoadBalancer();
    // 为每一个IP配置一个限流器
    private final Map<SocketAddress, RateLimiter> everyIpRateLimiter = new ConcurrentHashMap<>(16);
    // 为每一个IP配置一个断路器,做熔断
    private final Map<SocketAddress, CircuitBreaker> everyIpCircuitBreaker = new ConcurrentHashMap<>(16);

    // 分组信息
    private String group = "default";

    // 读xml
    public Configuration() {
        // 1、成员变量的默认配置项

        // 2、spi机制发现相关配置项
        SpiResolver spiResolver = new SpiResolver();
        spiResolver.loadFromSpi(this);
        // 3、读取xml获取上边的信息
        XmlResolver xmlResolver = new XmlResolver();
        xmlResolver.loadFromXml(this);
        // 4、编程配置项，wrpcBootstrap提供

    }

    public static void main(String[] args) {
        Configuration configuration = new Configuration();
    }


}
